#include "stdafx.h"
#include "PBGenie.h"
#include "helper.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CString IntToStr(int value)
{
	CString str;
	LPTSTR sz = str.GetBuffer(20);
#ifdef UNICODE
	_itow(value, sz, 10);
#else
	_itoa(value, sz, 10);
#endif
	str.ReleaseBuffer();
	return str;
}


CString UIntToStr(UINT value)
{
	CString str;
	LPTSTR sz = str.GetBuffer(20);
	wsprintf(sz, _T("%u"), value);
	str.ReleaseBuffer();
	return str;
}


CString BoolToStr(bool value)
{
	return (value) ? _T("True") : _T("False");
}


CString GetStringProperty(LPDISPATCH pDisp, LPCTSTR PropName)
{
	ASSERT(NULL != pDisp);
	USES_CONVERSION;
	CString str;

	CComDispatchDriver dispdrv(pDisp);
	VARIANT var;
	VariantInit(&var);
	HRESULT hr = dispdrv.GetPropertyByName(T2CW(PropName), &var);
	if (SUCCEEDED(hr))
	{
		switch (var.vt)
		{
		case VT_BSTR:
			str = var.bstrVal;
			break;
		case (VT_BSTR | VT_BYREF):
			str = *var.pbstrVal;
			break;
		}
		VariantClear(&var);
	}
	return str;
}


bool ExtensionIs(LPCTSTR FileName, LPCTSTR Ext)
{
	LPCTSTR szExt = _tcsrchr(FileName, _T('.'));
	if (!szExt) return false;
	if (lstrcmpi(szExt+1, Ext)==0)
		return true;
	else
		return false;
}


CString ChangeFileExt(LPCTSTR FileName, LPCTSTR Ext)
{
	CString str;
	LPTSTR lpsz = str.GetBuffer(_MAX_PATH);
	lstrcpy(lpsz, FileName);
	LPTSTR lpszExt = _tcsrchr(lpsz, _T('.'));
	if (lpszExt)
	{
		lstrcpy(lpszExt+1, Ext);
	}
	else
	{
		lstrcat(lpsz, _T("."));
		lstrcat(lpsz, Ext);
	}
	str.ReleaseBuffer();
	return str;
}


CString ComcatFileName(LPCTSTR Path, LPCTSTR FileName)
{
	CString str = Path;
	if (str.Right(1)!=_T("\\"))
		str += _T("\\");
	str += FileName;
	return str;
}
